import { resolve } from 'path'
import Vue from '@vitejs/plugin-vue'
import VueJsx from '@vitejs/plugin-vue-jsx'
import ViteLegacy from '@vitejs/plugin-legacy'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { visualizer as Visualizer } from 'rollup-plugin-visualizer'
import { viteVConsole as ViteVConsole } from 'vite-plugin-vconsole'
import ViteRestart from 'vite-plugin-restart'
import ViteImages from 'vite-plugin-vue-images'
import ViteImagemin from 'vite-plugin-imagemin'
import ViteCompression from 'vite-plugin-compression'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// import IconsResolver from 'unplugin-icons/resolver'
// import Icons from 'unplugin-icons/vite'

export default ({ cwd, notProd: enabled }) => {
  return [
    Vue(),
    VueJsx(),
    VueSetupExtend(),
    ViteLegacy({
      targets: ['defaults', 'not IE 11'],
    }),
    Visualizer({ open: false }),
    AutoImport({
      imports: ['vue', 'vue-router', 'pinia'], // 这些模块里面的api无需引入可直接使用
      dirs: [], // 自定义哪些模块无需导入
      dts: './auto-imports.d.ts', // 无需导入模块的类型文件地址
      resolvers: [
        ElementPlusResolver(),
        // IconsResolver({
        //   prefix: 'Icon',
        // }),
      ],
      eslintrc: {
        enabled: true,
        filepath: './.eslintrc-auto-import.json',
        globalsPropValue: true,
      },
    }),
    Components({
      dirs: [resolve(cwd)],
      dts: true,
      deep: true,
      extensions: ['vue'],
      resolvers: [
        ElementPlusResolver(),
        // IconsResolver({
        //   enabledCollections: ['ep'],
        // }),
      ],
    }),
    // Icons({
    //   autoInstall: true,
    // }),
    ViteRestart({
      restart: ['vite.config.ts'],
    }),
    ViteVConsole({
      entry: resolve(cwd, 'src/main.ts'),
      localEnabled: true,
      enabled,
      config: {
        maxLogNumber: 1000,
        theme: 'dark',
      },
    }),
    ViteImages({
      dirs: ['src/images'], // 图像目录的相对路径
      extensions: ['jpg', 'jpeg', 'png', 'svg', 'webp'], // 有效的图像扩展
      customResolvers: [], // 覆盖名称->图像路径解析的默认行为
      customSearchRegex: '([a-zA-Z0-9]+)', // 重写搜索要替换的变量的Regex。
    }),
    ViteImagemin({
      gifsicle: {
        optimizationLevel: 7,
        interlaced: false,
      },
      optipng: {
        optimizationLevel: 7,
      },
      mozjpeg: {
        quality: 20,
      },
      pngquant: {
        quality: [0.8, 0.9],
        speed: 4,
      },
      svgo: {
        plugins: [
          {
            name: 'removeViewBox',
          },
          {
            name: 'removeEmptyAttrs',
            active: false,
          },
        ],
      },
    }),
    ViteCompression({
      algorithm: 'gzip',
      threshold: 10240,
      verbose: false, // 是否在控制台输出压缩结果
      deleteOriginFile: false,
    }),
  ]
}
